package com.qeegoo.b2bautozityp.moudle.order.view;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.adapter.OrderAdapter;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.databinding.FragmentOrderBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerOrderListFragmentComponent;
import com.qeegoo.b2bautozityp.injector.module.OrderListFragmentModule;
import com.qeegoo.b2bautozityp.moudle.base.LazyLoadFragment;
import com.qeegoo.b2bautozityp.moudle.order.OrderDataHelper;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderResultBean;
import com.qeegoo.b2bautozityp.moudle.order.viewmodel.OrderListViewModel;

import java.util.List;

import javax.inject.Inject;

import base.lib.util.NavigateUtils;

/**
 * Created by lk on 2017/9/14.
 */

public class OrderReturnFragment extends LazyLoadFragment<FragmentOrderBinding> {


    @Inject
    LinearLayoutManager mLayuoutManager;

    @Inject
    OrderListViewModel viewModel;


    private int pageNo = 1;
    private int totalPages;
    private OrderResultBean resultData;

    public static OrderReturnFragment newInstance(int orderType) {

        Bundle args = new Bundle();
        args.putInt(Constants.Fragment_OrderType, orderType);
        OrderReturnFragment fragment = new OrderReturnFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e("OrderFragment", getArguments().get(Constants.Fragment_OrderType) + "");
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_order;
    }

    @Override
    protected void initInjector() {
        DaggerOrderListFragmentComponent.builder().appComponent(getAppComponent()).orderListFragmentModule(new OrderListFragmentModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        mBinding.orderRv.setLayoutManager(mLayuoutManager);
        mBinding.setViewModel(viewModel);
        mBinding.swipeLayout.setColorSchemeResources(R.color.colorPrimary);
        mBinding.swipeLayout.setOnRefreshListener(() -> {
            viewModel.getDatas1("1", getCurrentOrderType());
        });
        mBinding.orderRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
                    if (lastVisiblePosition >= linearLayoutManager.getItemCount() - 1) {
                        if (pageNo < totalPages) {
                            viewModel.getDatas1(++pageNo + "", getCurrentOrderType());
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            }

        });
        OrderAdapter orderAdapter = new OrderAdapter(Constants.AdapterType_1);
        mBinding.orderRv.setAdapter(orderAdapter);
        viewModel.setAdapterInterface(data -> {
            resultData = data;
            List<Object> datas = OrderDataHelper.handleOriginDatas(data.list,getCurrentOrderType());
            if (mBinding.swipeLayout != null) {
                mBinding.swipeLayout.setRefreshing(false);

            }
            if (data.list == null || data.list.size() == 0) {
                viewModel.emptyVisible.set(View.VISIBLE);
            } else {
                viewModel.emptyVisible.set(View.GONE);
            }
            if (data.curPageNo == 1) {
                orderAdapter.addAllDatas(datas);
            }else {
                orderAdapter.addDatas(datas);
            }
            totalPages = data.totalPages;
            pageNo = data.curPageNo;
        });
        orderAdapter.setOnItemClickListener(((view, position) -> {
            if (resultData !=null) {
                OrderResultBean.OrderListBean orderListBean = resultData.list.get(position);
                Bundle params = new Bundle();
                params.putString(Constants.Param_code, orderListBean.code);
                params.putInt(Constants.Fragment_OrderType, Constants.AdapterType_1);
                NavigateUtils.startActivity(OrderDetailActivity.class,params);
            }
        }));
    }

    @Override
    public void requestData() {
        mBinding.swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                mBinding.swipeLayout.setRefreshing(true);
            }
        });
        viewModel.getDatas1("1", getCurrentOrderType());
    }

    private String getCurrentOrderType() {
        if (getArguments() != null) {
            return getArguments().getInt(Constants.Fragment_OrderType) + "";
        }
        return "0";
    }

}
